package com.c.ctgproject.ui.files

import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.file.FileSecondExamineAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.file.SecondExamineCarEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.ui.vehiclefiles.VehiclefilesActivity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_file_second_examine.*
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2020/10/21
 *描述: 待二验车辆 页面 修改车辆信息列表 页面(初验之后二验之前)
 */
class FileSecondExamineActivity : BaseActivity(), FileSecondExamineAdapter.CarCallback {
    private val mViewModel: FileSecondExamineViewModel by lazy { ViewModelProvider(this)[FileSecondExamineViewModel::class.java] }
    private var page = 1
    private var size = 20
    private var searchText = ""
    private val mList = ArrayList<SecondExamineCarEntity.DataBean.ListBean>()
    private val mAdapter: FileSecondExamineAdapter by lazy { FileSecondExamineAdapter(this, mList, this) }
    private val isChangeCarInfo by lazy { intent.getBooleanExtra(MyConstants.CHANGE_CAR_INFO,false) }
    override fun event() {
        if (isChangeCarInfo) mTitleBar.setTitle("修改")
        changeTransparentStatusBar()
        mCarRV.adapter = mAdapter
        mCarRV.layoutManager = LinearLayoutManager(this)
        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("待二验车辆列表:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(it, SecondExamineCarEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showToast(data.msg)
            }
        })
    }

    override fun click() {
        mSearchET.handlerText {
            if (it.isEmpty()) {
                searchText = ""
                mList.clear()
                page = 1
                getData()
            }
        }

        //搜索
        mSearchTV.setOnClickListener {
            searchText = mSearchET.text.toString()
            page = 1
            mList.clear()
            getData()
        }

        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }
        mRefresh.setOnRefreshListener {
            page = 1
            mList.clear()
            getData()
        }
    }

    override fun onResume() {
        super.onResume()
        page = 1
        searchText = ""
        mList.clear()
        getData()
    }

    private fun getData() {
        loading.show()
        mViewModel.getCarList(page, size, searchText)
    }

    override fun getLayout() = R.layout.activity_file_second_examine

    override fun onItemClick(position: Int) {
        val data=mList[position]
        if (isChangeCarInfo){
            startActivity<ChangeCarInfoActivity>(
                MyConstants.CAR_ID to data.carId.handlerNull(),
                MyConstants.CAR_OWNER to data.carOwner.handlerNull(),
                MyConstants.CAR_VIN to data.carVin.handlerNull(),
                MyConstants.CAR_PLATE to data.ownerPlate.handlerNull()
            )
        }else{
            val intent = Intent(applicationContext, VehiclefilesActivity::class.java)
            intent.putExtra("carCode", data.carCode.handlerNull())
            startActivity(intent)
        }
    }
}